package org.jeecg.modules.front.pay;

import com.alibaba.fastjson.JSON;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.front.util.WechatUtil;
import org.springframework.stereotype.Service;
import com.wechat.pay.java.core.Config;

import java.util.Map;

@Service
public class WechatScan extends Payment {

    public Result<JSONObject> pay(String requestNo, Double amount, Map<String, String> extendParams) {
        // 先构建请求客户端
        Config config = WechatUtil.getConfig();
        // 构建service
        NativePayService service = new NativePayService.Builder().config(config).build();
        // request.setXxx(val)设置所需参数，具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        Amount wechatAmount = new Amount();
        wechatAmount.setTotal((int) (amount*100));
        request.setAmount(wechatAmount);
        request.setAppid(WechatUtil.getAppId());
        request.setMchid(WechatUtil.getMerchantId());
        request.setDescription((extendParams.get("name")));
        request.setNotifyUrl(WechatUtil.getNotifyUrl()+"front/order/wx/notify");
        request.setOutTradeNo(requestNo);
        // 调用下单方法，得到应答
        // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
        PrepayResponse codeUrl = service.prepay(request);
        return  Result.ok(codeUrl.getCodeUrl());

    }

    public Integer payType() {
        return 1;
    }
}
